#import "TopControl.h"
#import "ScaledView.h"

#import <dpsclient/dpsclient.h>
#import <appkit/Application.h>
#import <appkit/Window.h>
#import <appkit/graphics.h>
#import <appkit/NXBitmapImageRep.h>
#import <appkit/OpenPanel.h>

@implementation TopControl
- appDidInit:sender
{
  NXRect frame;
  [[window contentView] getFrame:&frame];
  frame.origin.x=0.0;
  frame.origin.y=0.0;

  // currently hardwired to ScaledView.  Simply add whatever
  // view you want to the project and grab a new instance here.  Everything
  // should work just dandy after that.
  view1=[[ScaledView alloc] initFrame:&frame];
  [view1 setAutosizing:NX_WIDTHSIZABLE | NX_HEIGHTSIZABLE];
  [[window contentView] addSubview:view1];
  [window useOptimizedDrawing:YES];
  [window setBackgroundGray:NX_BLACK];
  return self;
}

- doIE
{
  NXEvent dummyEvent, *evnt;

  [infoView lockFocus];
  do {
    [infoView oneStep];
    [infoView oneStep]; // do two steps

    evnt=[NXApp peekNextEvent:NX_ALLEVENTS into:&dummyEvent waitFor:0
	  threshold:NX_BASETHRESHOLD];
  } while(infoRun && !evnt);
    
  [infoView unlockFocus];
  return self;
}  

void itehandler(DPSTimedEntry te, double time, void *tobj)
{ [(id)tobj doIE]; }

- infoPanel:sender
{
  [infoPanel makeKeyAndOrderFront:self];
  if(!infoRun){
    ientry=DPSAddTimedEntry(0.02, &itehandler, self, NX_BASETHRESHOLD);
    infoRun=YES;
  }
  return self;
}

- windowWillClose:sender
{
  if (sender==infoPanel) {
    infoRun=NO;
    DPSRemoveTimedEntry(ientry);
  }
  return self;
}

- doTE
{
  NXEvent dummyEvent, *evnt;
  NXEvent *theEvent, nextEvent;
  char bname[50], name[100];
  NXBitmapImageRep *theRep;
  NXStream *theStream;
  NXRect theRect;
  int count=0;
  BOOL advancedCommandSet=[view1 advancedCommandSet];
  
  sprintf(bname,"/tmp/%s", [view1 name]);
  
  [view1 lockFocus];
  do {
    [view1 oneStep];
    [view1 oneStep]; // do two steps

    // this if statement basically does some advanced sort of stuff if the
    // view supports it.  It currently isn''t documented, but will be in the
    // future. Check out FooFaraw.m and ScaledView.m for a view that supports
    // such goofiness...
    if(advancedCommandSet &&
       [NXApp peekNextEvent:NX_FLAGSCHANGEDMASK
	into:&nextEvent]){
      theEvent=[NXApp getNextEvent:NX_FLAGSCHANGEDMASK];
      if(theEvent->flags & NX_COMMANDMASK) {
	if([view1
	    respondsTo:@selector(commandKey)]){
	  [view1 commandKey];
	}
      } else
	if(theEvent->flags & NX_ALTERNATEMASK) {
	  [view1 getBounds:&theRect];
	  theRep = [[NXBitmapImageRep alloc]
		    initData:NULL fromRect:&theRect];
	  sprintf(name, "%s.%d.tiff", bname, count++);
	  fprintf(stdout, "BackSpace (%s) Saving: %s\n",
		  [view1 name], name);
	  theStream=NXOpenMemory(NULL, 0, NX_WRITEONLY);
	  if(theStream) {
	    [theRep writeTIFF:theStream];
	    if(NXSaveToFile(theStream, name) != 0) {
	      fprintf(stderr, "FooFaraw:save failed!\n");
	    }
	    NXClose(theStream);
	  } else {
	    fprintf(stderr,
		    "FooFaraw: could not save!\n");
	  }
	  [theRep free];
	}
    }
    evnt=[NXApp peekNextEvent:NX_ALLEVENTS into:&dummyEvent waitFor:0
	  threshold:NX_BASETHRESHOLD];
  } while(running && !evnt);
    
  [view1 unlockFocus];
  return self;
}

void tehandler(DPSTimedEntry te, double time, void *tobj)
{ [(id)tobj doTE]; }

- runStop:sender
{
  if(!running){
    // add the timed entry
    tentry=DPSAddTimedEntry(0.02, &tehandler, self, NX_BASETHRESHOLD);
    running=YES;
  } else {
    running=NO;
    DPSRemoveTimedEntry(tentry);
  }
  return self;
  
/*   [view1 lockFocus];
  for(i=0;i<1000;i++)
    [view1 oneStep]; 
  
  [view1 unlockFocus];
  */
}

- open:sender
{
    return self;
}


@end
